/**
 *
 */
package org.insia.smartposse.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.insia.smartposse.metier.User;

/**
 * @author raguerri
 * <p>
 * Project : Smartposse <br>
 * Class : org.insia.vmail.session.filter <br>
 * File : AuthFilter.java
 * </p>
 */
public class AuthFilter 
implements Filter {
	
    /**
     * Method init
     *
     * @param filterConfig
     * @throws ServletException when
     */
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    /**
     * Method doFilter
     *
     * @param request
     * @param response
     * @param chain
     * @throws IOException when
     * @throws ServletException when
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        String loginPage = req.getContextPath() + "/Welcome.do";
        String login = req.getContextPath() + "/login.do";
        String createFreelance = req.getContextPath() + "/createFreelance.do";
        String messagePage = req.getContextPath() + "/messagePage.do";
        String createCompany = req.getContextPath() + "/createCompany.do";
        String changeLocale = req.getContextPath() + "/changeLocale.do";
        String retreiveLoginPassword = req.getContextPath() + "/retreiveLoginPassword.do";
        User user = (User) session.getAttribute("user");
        String jsession = (String) session.getAttribute("jsessionid");
        if (user == null && (!req.getRequestURI().equals(loginPage))
        		&& (!req.getRequestURI().equals(login))
        		&& (!req.getRequestURI().equals(createFreelance))
        		&& (!req.getRequestURI().equals(messagePage))
        		&& (!req.getRequestURI().equals(createCompany))
        		&& (!req.getRequestURI().equals(changeLocale))        		
        		&& (!req.getRequestURI().equals(retreiveLoginPassword)) ) {
            if (jsession != null)
                session.removeAttribute("jsessionid");
            req.getRequestDispatcher("/jsp/pages/Welcome.jsp").forward(req, res);
        } else if (user == null && ((req.getRequestURI().equals(loginPage))
        		|| req.getRequestURI().equals(login)
        		|| req.getRequestURI().equals(createFreelance)
        		|| req.getRequestURI().equals(messagePage)
        		|| req.getRequestURI().equals(createCompany)
        		|| req.getRequestURI().equals(changeLocale)
        		|| req.getRequestURI().equals(retreiveLoginPassword) ) ) {
            if (jsession != null)
                session.removeAttribute("jsessionid");
            chain.doFilter(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    /**
     * Method destroy
     *
     */
    public void destroy() {
    }
}
